const {
	importTemplateData,
	loadTemplateDetail,
} = require('./api-template-methods')
const {
	dialogClose,
} = require('./api-utils')

module.exports = function(apiComp) {
	let {
		templateDialog,
		templateTree,
		templateTable,
	} = apiComp

	// 初始化模板对话框
	templateDialog.dialog({
		title: '模板列表',
		top: 50,
		left: '10%',
		width: '80%',
		height: '80%',
		closed: true,
		modal: true,
		buttons: [{
			text: '导入',
			iconCls: 'icon-redo',
			handler: function() {
				importTemplateData.call(apiComp)
			}
		}, {
			text: '取消',
			iconCls: 'icon-cancel',
			handler: function() {
				templateDialog.dialog('close')
			}
		}],
		onClose() {
			dialogClose.call(apiComp)
		},
	})

	// 初始化模板目录
	templateTree.tree({
		onClick(node) {
			loadTemplateDetail.call(apiComp, node)
		}
	})

	// 初始化模板列表
	templateTable.treegrid({
		title: '模板参数',
		fit: true,
		fitColumns: true,
		treeField: 'name',
		lines: true,
		idField: '_treeId_',
		dblclickToEdit: false,
		clickToEdit: false,
		columns: [
			[{
				field: 'name',
				title: '变量名',
				width: 60,
			}, {
				field: 'mandatory',
				title: '必填',
				width: 15,

			}, {
				field: 'content',
				title: '含义',
				width: 60,

			}, {
				field: 'type',
				title: '类型',
				width: 30,
			}, {
				field: 'remark',
				title: '备注',
				width: 50,
			}]
		],
	})
}